import 'package:flutter/material.dart';
import 'package:flutter_stort/config/routes.dart';
import 'package:flutter_stort/html/address_dao.dart';
import 'package:flutter_stort/model/address_entity.dart';
import 'package:flutter_stort/utils/event_bus.dart';
import 'package:flutter_stort/utils/jump_util.dart';
import 'package:flutter_stort/widget/load_state_view.dart';

import 'edit_address_page.dart';

///收货地址
class ShippingAddressPage extends StatefulWidget {
  @override
  _ShippingAddressPageState createState() => _ShippingAddressPageState();
}

class _ShippingAddressPageState extends State<ShippingAddressPage> {
  LoadState _loadState = LoadState.LOADING;
  List<AddressEntity> _listData = [];

  @override
  void initState() {
    _loadState = LoadState.LOADING;
    EventBusUtils.eventBus.on<AddressEntity>().listen((value) {
      setState(() {
        if (value is AddressEntity) {
          _listData.add(value);
        }
      });
    });
    AddressDao.getAddress().then((value) {
      setState(() {
        _listData.addAll(value);
        _loadState = LoadState.SUCCESS;
      });
    });
    super.initState();
  }

  ///列表项
  Widget _addressItem(BuildContext context, int index) {
    return Container(
      padding: EdgeInsets.all(10),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            width: 40,
            height: 40,
            alignment: Alignment.center,
            child: Icon(
              Icons.home,
              color: Colors.white,
              size: 30,
            ),
            decoration: BoxDecoration(
              color: Colors.orange,
              borderRadius: BorderRadius.circular(20),
            ),
          ),
          SizedBox(width: 10),
          Expanded(
              flex: 1,
              child: Container(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text.rich(
                      TextSpan(
                        text: "${_listData[index].name}\t\t",
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 16,
                        ),
                        children: [
                          TextSpan(
                            text: _listData[index].mobile,
                            style: TextStyle(
                              color: Colors.grey,
                              fontSize: 12,
                            ),
                          ),
                        ],
                      ),
                    ),
                    Text(
                      "${_listData[index].province}${_listData[index].city}${_listData[index].area}${_listData[index].detailedAddress}",
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(color: Colors.black, fontSize: 12),
                    )
                  ],
                ),
              )),
          InkWell(
              onTap: () {
                JumpPageUtils.startNextWidget(
                  context,
                  EditAddressPage(address: _listData[index]),
                );
              },
              child: Container(
                width: 40,
                height: 70,
                alignment: Alignment.center,
                child: Text(
                  "编辑",
                  style: TextStyle(
                    color: Colors.grey,
                    fontSize: 14,
                  ),
                ),
              )),
        ],
      ),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(
          bottom: BorderSide(
            color: Colors.grey,
            width: 0.5,
            style: BorderStyle.solid,
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "收货地址管理",
          style: TextStyle(
            color: Colors.white,
            fontSize: 20,
          ),
        ),
        actions: [
          FlatButton(
            onPressed: () =>
                JumpPageUtils.startNext(context, RoutesUtils.EDIT_ADDRESS_ROOT),
            child: Text(
              "添加新地址",
              style: TextStyle(
                color: Colors.white,
                fontSize: 14,
              ),
            ),
          ),
        ],
      ),
      body: LoadStateView(
        loadState: _loadState,
        successWidget: ListView.builder(
          itemBuilder: _addressItem,
          itemCount: _listData.length,
        ),
        erroRetry: () {},
      ),
    );
  }
}
